package jsu.xxy.coffee3.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jsu.xxy.coffee3.domain.Milktea;
import jsu.xxy.coffee3.service.MilkteaService;
import jsu.xxy.coffee3.mapper.MilkteaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
* @author 张从轩
* @description 针对表【milktea】的数据库操作Service实现
* @createDate 2023-12-30 08:15:39
*/
@Service
public class MilkteaServiceImpl extends ServiceImpl<MilkteaMapper, Milktea>
    implements MilkteaService{
    @Autowired
    MilkteaMapper milkteaMapper;

    @Override
    public List<Milktea> recommend() {
//        随机选择4款向用户推荐
        int nums = 4;
        int start = 1;
        int end = countMilkteas();
        //1.创建集合容器对象
        List list = new ArrayList();
        List ret = new ArrayList();
        //2.创建Random对象
        Random r = new Random();
        //循环将得到的随机数进行判断，如果随机数不存在于集合中，则将随机数放入集合中，如果存在，则将随机数丢弃不做操作，进行下一次循环，直到集合长度等于nums
        while (list.size() != nums) {
            int num = r.nextInt(end - start) + start;
            if (!list.contains(num)) {
                list.add(num);
                System.out.println(num);
            }
        }
        for (Object l : list) {
            NumberFormat formatter = NumberFormat.getNumberInstance();
            formatter.setMinimumIntegerDigits(4);
            formatter.setGroupingUsed(false);
            String s = formatter.format(Integer.parseInt(l.toString()));
            System.out.println(s);
            ret.add(selectOneMilktea(s));
        }
        return ret;
    }

    @Override
    public Milktea selectOneMilktea(String id) {
        Milktea m = milkteaMapper.selectMilkteaById(id);
        System.out.println(m.toString());
        return m;
    }

    @Override
    public List<Milktea> selectAllMilktea() {
        return milkteaMapper.selectAll();
    }

    @Override
    public List<Milktea> selectByType(String type) {
        List<Milktea> l = milkteaMapper.selectByType(type);
        for(int i = 0 ; i < l.size() ; i ++){
            System.out.println(l.get(i));
        }
        return l;
    }

    @Override
    public int countMilkteas() {
//        System.out.println("11");
        int t = milkteaMapper.countMilktea();
//        System.out.println("TTTTTTTTTTTTTTTTT");
        System.out.println(t);
        return t;
    }

    @Override
    public int countType() {
        return milkteaMapper.countType();
    }

    @Override
    public String getImages(String drinkId) {
        return milkteaMapper.getImgById(drinkId);
    }

    @Override
    public List<List<Milktea>> selectAllByType(String type) {
        List<List<Milktea>> res= new ArrayList<>();

        for (int i = 0; i < 4; i++) {
            List<Milktea> temp = selectByType(Integer.toString(i+1));
//            coffee_type l = new coffee_type(temp.get(0).getType(),temp.get(0).getType_name(),temp);
//            res.add(l);
            for (int j=0;j<temp.size();j++){
                System.out.println(temp.get(j));
            }
            res.add(temp);
        }
        return res;
    }

}




